Sử dụng hàm drupal_add_tabledrag?

Events happening in the community are now at Drupal community events on www.drupal.org.
myvo's picture

Hi all,
Hiện tại mình muốn làm một page giống page: admin/build/menu-customize/navigation . Mình đã đọc hướng dẫn sử dụng module drupal_add_tabledrag() rất nhiều lần nhưng vẫn chưa làm được 1 page tương tự page sắp xếp lại menu navigation theo dạng match, parent.
Rất mong nhận được sự trợ giúp từ mọi người.

Comments

Cảm ơn câu hỏi của bạn, Từ

thehong's picture

Cảm ơn câu hỏi của bạn,

Từ trước đến giờ tôi chỉ làm drag drop xuông thôi, trở ngại của bạn làm tôi có chút tò mò, và hên thành công: http://toilanet.pastebin.com/Emig6WZp

Code nhanh nên không comment gì ráo, bạn không hiểu chỗ nào thì hỏi nhé.

Thế Hồng

Cảm ơn Thể Hồng

myvo's picture

Cảm ơn Thể Hồng rất nhiều. Nhưng sao mình add "submit" button vào mà ko được, Form ko hiểu.
Thank you so much.

--
Mỹ VÕ

Chưa thể lưu được cấu trúc "cha-con"

myvo's picture

Hiện tại mình đã add được button submit. Nhưng sau khi submit mình không thể lấy được cấu trúc "cha-con" và thứ tự xuất hiện (weight). Làm sao để có thể làm được việc này giống như khi edit menu "Navigation"?

--
Mỹ VÕ

Hiển thị dữ liệu theo cấu trúc "cha-con"

myvo's picture

Hiện tại mình đã có thể lấy được cấu trúc "cha-con" sau khi submit và weight, nhưng lại gặp vấn đề khi hiển thị dữ liệu theo cấu trúc ấy. Mình tham khảo nhiều lần module menu nhưng vẫn chưa hiểu cơ chế hiển thị như thế nào.
Rất mong được sự giúp đỡ.
My code:

<?php
//the same menu module.

function draggabledemo_menu() {
 
$items = array();

 
$items['draggableparent'] = array(
   
'title' => 'Draggable Sorting List Demo',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('draggabledemo_sort_form_parent'),
   
'access arguments' => array('access content'),
  );
  return
$items;
}


function
draggabledemo_sort_form_parent() {
   
$data = array(
    
'zero'        => 'Zero',
       
'first'       => 'First',
      
'second'      => 'Second',
     
'third'       => 'Third',
      
'forth'       => 'Fourth',
     
'fiveth'      => 'Fiveth',
  );
$list = array(
    
'zero'        => array('weight' => -6, 'mlid' => 'zero',   'plid' => 0),
    
'first'   => array('weight' => -5, 'mlid' => 'first', 'plid' => 'zero'),
      
'second'  => array('weight' => -6, 'mlid' => 'second','plid' => 'first'),
     
'third'   => array('weight' => -6, 'mlid' => 'third', 'plid' => 'second'),
    
'forth'   => array('weight' => -6, 'mlid' => 'forth', 'plid' => 'zero'),
      
'fiveth'  => array('weight' => -4, 'mlid' => 'fiveth','plid' => 'zero')
    );

  
$form = _show_tree_test($data, $list);
//drupal_set_message('<pre>'.print_r($form, 1).'</pre>');

 
if (element_children($form)) {
   
$form['submit'] = array(
     
'#type' => 'submit',
     
'#value' => t('Save configuration'),
    );
  }
  else {
   
$form['empty_vocabulary'] = array('#value' => t('There are no menu items yet.'));
  }
  return
$form;
}

function
_show_tree_test($data, $list) {
 
//$form = array();
static $form = array('#tree' => TRUE);
    foreach (
$data AS $key => $title) {
    
$form[$key]['#attributes'] = array('class' => 'menu-enabled');
       
$form[$key]['title'] = array('#value' => $title);
      
$form[$key]['weight'] = array('#type' => 'weight', '#delta' => count($data), '#default_value' => $list[$key]['weight']);
     
$form[$key]['mlid'] = array(
        
'#type' => 'hidden',
           
'#delta' => 50,
          
'#default_value'  => $list[$key]['mlid'],
       );
    
$form[$key]['plid'] = array(
        
'#type' => 'textfield',
        
'#delta' => 50,
          
'#default_value'  => $list[$key]['plid'],
          
'#size'       => 6,
       );
}
 
   return
$form;
}


function
theme_draggabledemo_sort_form_parent($form) {
//drupal_set_message('FORM<pre>'.print_r($form, 1).'</pre>');
 
drupal_add_tabledrag('myvo-draggable-demo', 'match', 'parent', 'menu-plid', 'menu-plid', 'menu-mlid', TRUE, 0);
 
drupal_add_tabledrag('myvo-draggable-demo', 'order', 'sibling', 'menu-weight');

 
$header = array(
   
t('Title'),
   
'',
  );

 
$rows = array();
 
$i= 0;
  foreach (
element_children($form) as $mlid) {
    if (isset(
$form[$mlid]['mlid'])) {
  
$element = &$form[$mlid];
 
//drupal_set_message($mlid.'FORM<pre>'.print_r($element, 1).'</pre>');
//drupal_set_message('OK: '.$i++);
      // Add special classes to be used for tabledrag.js.
     
$element['plid']['#attributes']['class'] = 'menu-plid';
     
$element['mlid']['#attributes']['class'] = 'menu-mlid';
     
$element['weight']['#attributes']['class'] = 'menu-weight';

     
// Change the parent field to a hidden. This allows any value but hides the field.
     
$element['plid']['#type'] = 'hidden';

     
$row = array();
     
$row[] = theme('indentation', $element['depth'] - 1) . drupal_render($element['title']);
     
$row[] = drupal_render($element['weight']) . drupal_render($element['plid']) . drupal_render($element['mlid']);

   
$row = array_merge(array('data' => $row), $element['#attributes']);
     
$row['class'] = !empty($row['class']) ? $row['class'] .' draggable' : 'draggable';
     
$rows[] = $row;
    }
  }
 
//drupal_set_message('<pre>'.print_r($element, 1).'</pre>');
 
$output = '';
  if (
$rows) {
   
$output .= theme('table', $header, $rows, array('id' => 'myvo-draggable-demo'));
  }
 
$output .= drupal_render($form);
  return
$output;
}

function
draggabledemo_sort_form_parent_submit($form, &$form_state) {
   
drupal_set_message('<pre>'.print_r($form_state, 1).'</pre>');
}

/**
* Implementation of hook_theme
*/
function draggabledemo_theme() {
  return array(
   
'draggabledemo_sort_form_parent'  => array(
      
'arguments' => array('form' => NULL),
    ),
  );
}
?>

--
Mỹ VÕ

myvo's picture

Để có thể hiển thị được cấu trúc "cha-con" mình cần phải có depth + theme('indentation', $element['#item']['depth'] - 1)
Để biết được item nào trên, item nào dưới, cần phải có weigth.
Việc kế tiếp là sort thứ tự hiển thị theo depth và weight.

<?php
/<strong>
*
Implementation of hook_menu
*/
function
draggabledemo_menu() {
 
$items = array();

 
$items['draggableparent'] = array(
   
'title' => 'Draggable Sorting List Demo',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('draggabledemo_sort_form_parent'),
   
'access arguments' => array('access content'),
  );

  return
$items;
}


function
draggabledemo_sort_form_parent() {
  
$data = array(
    
'forth'       => 'Fourth',
     
'fiveth'      => 'Fiveth',
     
'third'       => 'Third',
      
'zero'        => 'Zero',
       
'first'       => 'First',
      
'second'      => 'Second',
  );

  
$list = array(
    
'forth'   => array('weight' => -1, 'mlid' => 'forth', 'plid' => 0, 'depth'  => 1),
     
'fiveth'  => array('weight' => 0, 'mlid' => 'fiveth','plid' => 0, 'depth'   => 1),
     
'third'   => array('weight' => 5, 'mlid' => 'third', 'plid' => 0, 'depth'   => 1),
     
'zero'        => array('weight' => 6, 'mlid' => 'zero',    'plid' => 0, 'depth' => 1),
     
'first'   => array('weight' => -5, 'mlid' => 'first', 'plid' => 'zero', 'depth' => 2),
       
'second'  => array('weight' => 1, 'mlid' => 'second','plid' => 'first', 'depth'   => 3),
  );

  
$form = _show_tree_test($data, $list);
 
  if (
element_children($form)) {
   
$form['submit'] = array(
     
'#type' => 'submit',
     
'#value' => t('Save configuration'),
    );
  }
  else {
   
$form['empty_item'] = array('#value' => t('There are no menu items yet.'));
  }
  return
$form;
}

function
_show_tree_test($data, $list) {
//$form = array();
static $form = array('#tree' => TRUE);
    foreach (
$data AS $key => $title) {
    
$form[$key]['#attributes'] = array('class' => 'menu-enabled');
       
$form[$key]['#item']['depth'] = $list[$key]['depth'];
       
$form[$key]['title'] = array('#value' => $title);
      
$form[$key]['weight'] = array('#type' => 'weight', '#delta' => count($data), '#default_value' => $list[$key]['weight']);
     
$form[$key]['mlid'] = array(
        
'#type' => 'hidden',
           
'#delta' => 50,
          
'#default_value'  => $list[$key]['mlid'],
       );
    
$form[$key]['plid'] = array(
        
'#type' => 'textfield',
        
'#delta' => 50,
          
'#default_value'  => $list[$key]['plid'],
          
'#size'       => 6,
       );
}
  return
$form;
}


function
theme_draggabledemo_sort_form_parent($form) {
//drupal_set_message('FORM<pre>'.print_r($form, 1).'</pre>');
 
drupal_add_tabledrag('myvo-draggable-demo', 'match', 'parent', 'menu-plid', 'menu-plid', 'menu-mlid', TRUE, 0);
 
drupal_add_tabledrag('myvo-draggable-demo', 'order', 'sibling', 'menu-weight');

 
$header = array(
   
t('Title'),
   
'',
  );

 
$rows = array();
 
$i= 0;
  foreach (
element_children($form) as $mlid) {
    if (isset(
$form[$mlid]['mlid'])) {
  
$element = &$form[$mlid];
 
//drupal_set_message($mlid.'--FORM<pre>'.print_r($element, 1).'</pre>');
   //drupal_set_message('OK: '.$i++);
      // Add special classes to be used for tabledrag.js.
     
$element['plid']['#attributes']['class'] = 'menu-plid';
     
$element['mlid']['#attributes']['class'] = 'menu-mlid';
     
$element['weight']['#attributes']['class'] = 'menu-weight';

     
// Change the parent field to a hidden. This allows any value but hides the field.
     
$element['plid']['#type'] = 'hidden';

     
$row = array();
     
$row[] = theme('indentation', $element['#item']['depth'] - 1) . drupal_render($element['title']);
     
$row[] = drupal_render($element['weight']) . drupal_render($element['plid']) . drupal_render($element['mlid']);

    
$row = array_merge(array('data' => $row), $element['#attributes']);
     
$row['class'] = !empty($row['class']) ? $row['class'] .' draggable' : 'draggable';
     
$rows[] = $row;
    }
  }
 
//drupal_set_message('<pre>'.print_r($element, 1).'</pre>');
 
$output = '';
  if (
$rows) {
   
$output .= theme('table', $header, $rows, array('id' => 'myvo-draggable-demo'));
  }
 
$output .= drupal_render($form);
  return
$output;
}

function
draggabledemo_sort_form_parent_submit($form, &$form_state) {
   
drupal_set_message('SUBMIT<pre>'.print_r($form_state, 1).'</pre>');
}

/</
strong>
*
Implementation of hook_theme
*/
function
draggabledemo_theme() {
  return array(
 
'draggabledemo_sort_form_parent'  => array(
      
'arguments' => array('form' => NULL),
    ),
  );
}
?>

--
Mỹ VÕ

Giúp mình với!

dieu's picture

Trong khi cài đặt drupal với vertrigo mình bị thông báo lỗi:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY fit DESC LIMIT 0, 1' at line 1 query: SELECT * FROM menu_router WHERE path IN () ORDER BY fit DESC LIMIT 0, 1 in C:\Program Files\VertrigoServ\www\drupal-6.16\includes\menu.inc on line 315.
user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY fit DESC LIMIT 0, 1' at line 1 query: SELECT * FROM menu_router WHERE path IN () ORDER BY fit DESC LIMIT 0, 1 in C:\Program Files\VertrigoServ\www\drupal-6.16\includes\menu.inc on line 315.

Mong câu trả lời sớm. Thanks

Thanks. díky

michal-3D's picture

you saved my day (-:

Cảm ơn các bạn đã có code cho

luatviettin's picture

Cảm ơn các bạn đã có code cho tớ xài chùa !